package com.zte.claa.inficombo.csa.app.service.redis;

import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

public abstract class DefaultMsgListener implements MessageListener {
	public static final String DTFormat = "yyyy-MM-dd HH:mm:ss";
	
	private Logger logger = LoggerFactory.getLogger(DefaultMsgListener.class);
			
	// 日期格式化.
	protected SimpleDateFormat dateFormater = new SimpleDateFormat(DTFormat);
	
	protected RedisMsgListenService listenService;
	
	public DefaultMsgListener(RedisMsgListenService listenService){
		this.listenService = listenService;
	}
			
    @Override
    public void onMessage( final Message message, final byte[] pattern ) {
    	try{
    		String channel = new String(message.getChannel(), "UTF-8");    
    		String msg = new String(message.getBody(), "UTF-8");    		
    		this.processMsgData(msg);
    		logger.info("[*Redis*]Receive msg: channel="+channel+", data="+msg);
    	}catch(Exception e){
    		logger.error("Parse redis message failed: ", e);
    	}
    }
    
    /**
     * 有效消息载荷处理，由子类具体实现
     * 
     * @param jsonMsg
     */
    abstract public void processMsgData(String jsonMsg);
}
